<script setup>
import {reactive, ref} from "vue";
import { useRouter } from 'vue-router';
import request from "@/utils/request";
import {useTokenStore} from "@/utils/token";
import {md5} from "js-md5";
const router = useRouter();
const tokenStore = useTokenStore()
//表单数据
let form=reactive({
  username:'',
  password:'',
  character:1,
  code:''
})
//表单规则
const rules = reactive({
  username:[
    {min: 1, max: 16, message: '用户名长度为1~16', trigger: 'blur' },
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password:[
    {min: 6, max: 16, message: '密码长度为6~16', trigger: 'blur' },
    { required: true, message: '密码不能为空', trigger: 'blur' }
  ],
  code:[
    { required: true, message: '验证码不能为空', trigger: 'blur' }
  ]
})
//表单实例
const ruleFormRef = ref()
//验证码图片url地址
let codeImgUrl = ref("http://localhost:8090/login/getCode")
//登录接口地址
let loginUrl = ref("http://localhost:8090/login/submit")
//登录函数
function Submit(formEl){
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      let params={
        username:form.username,
        password:md5(form.password),
        character:form.character,
        code:form.code
      }
      request.post(loginUrl.value,params)
          .then(res=>{
            if(res.data.code === 300){
              alert(res.data.msg)
            }
            else if(res.data.code === 200){
              //保存token
              console.log(res.data.data)
              tokenStore.setToken(res.data.data)
              if(params.character === 1)router.push("/user/home")
              else router.push("/admin/home")
            }
          })
          .catch(err=>{
            console.log(err)
          })
    }
  })

}
//点击验证码图片刷新验证码
function RefreshCodeImage(){
  codeImgUrl.value = codeImgUrl.value + "?" + Math.random().toString()
}

</script>

<template>
  <div class="page">
    <div class="container">
      <div class="top">
        <el-avatar :size="80" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" style="margin-top: 5px"/>
      </div>
      <div class="center">
        <div class="form">
          <el-form :model="form" :rules="rules" ref="ruleFormRef" status-icon>
            <el-form-item prop="username">
              <el-input v-model="form.username" placeholder="登录用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="form.password" placeholder="登录密码" type="password"></el-input>
            </el-form-item>
            <el-form-item class="code" prop="code">
              <el-container style="margin-right: 10px">
                <el-input v-model="form.code" placeholder="验证码"></el-input>
              </el-container>
              <el-container>
                <el-image style="background-color:pink;width: 100px;height: 40px"
                          :src="codeImgUrl"
                          @click="RefreshCodeImage"
                ></el-image>
              </el-container>

            </el-form-item>
            <el-form-item label="登录角色">
              <el-radio-group v-model="form.character" >
                <el-radio :label="1">用户</el-radio>
                <el-radio :label="2">管理员</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button @click="Submit(ruleFormRef)" type="primary">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="bottom">
        <a href="/register">立即注册</a>
        <a href="/register">忘记密码</a>
      </div>
    </div>
  </div>
</template>

<style scoped>

.page {
  padding-top: 100px;
  width: 100%;
  height: 100%;
  background:url("../../../public/image/loginbg.webp") no-repeat;
  background-size: cover;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 400px;
  height: 400px;
  background-color: rgba(1,1,1,.2);
  margin: 0 auto;
  border: #e9eef3 solid 1px;
  border-radius: 20px;
}

.top {
  width: 100%;
  height: 20%;
}

.center {
  width: 100%;
  height: 70%;
}

.bottom {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding-left: 10px;
  width: 95%;
  height: 10%;
}

.el-form-item {
  width: 80%;
  height: 35px;
  margin: 20px auto;
}

.el-radio-group {
  margin: 0 auto;
  color: white;
}

.code {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.el-button {
  margin: 0 auto;
  width: 80%;
  border-radius: 20px;
}

a {
  text-decoration: none;
  color: white;
}
a:hover {
  color: skyblue;
}


</style>